﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ReferralTracker.Data.Entities;
using ReferralTracker.BusinessObjects.Infrastructure;
using ReferralTracker.BusinessObjects;

namespace ReferralTracker.Controllers {
    public class AdminController : Controller {

        public ActionResult Index() {
            var admin = new Administration();
            var siteList = admin.GetAllSites();

            admin.Dispose();

            return View(siteList);
        }

        [HttpPost]
        public JsonResult AddSite(ReferralSite site) {
            var success = false;
            var errMsg = "";

            try {
                var session = ReferralTrackerSession.Open();
                var admin = new Administration();

                admin.RegisterSite(site);
                session.Commit();
                session.Close();
                success = true;
            }
            catch (Exception ex) {
                errMsg = ex.Message;
            }

            return Json(new { success = success, errMsg = errMsg, data = site });
        }

        [HttpPost]
        public JsonResult GetSite(int Id) {
            var admin = new Administration();
            var site = admin.GetSite(Id);

            admin.Dispose();

            return Json(site);
        }

        [HttpPost]
        public JsonResult UpdateSite(ReferralSite site) {
            var session = ReferralTrackerSession.Open();
            var admin = new Administration();
            var origSite = admin.GetSite(site.Id);
            var success = false;
            var errMsg = "";

            if (origSite != null) {
                origSite.Name = site.Name;
                origSite.RootUrl = site.RootUrl;
                origSite.ReferralLinkPattern = site.ReferralLinkPattern;

                session.Commit();
                session.Close();
                success = true;
            }
            else {
                errMsg = "Error updating site";
            }

            return Json(new { success = success, errMsg = errMsg, data = site });
        }

        [HttpPost]
        public JsonResult DeleteSite(int Id) {
            var success = false;
            var errMsg = "";

            try {
                var session = ReferralTrackerSession.Open();
                var admin = new Administration();

                admin.DeleteSite(Id);
                session.Commit();
                session.Close();
                success = true;
            }
            catch (Exception ex) {
                errMsg = ex.Message;
            }

            return Json(new { success = success, errMsg = errMsg });
        }

        [HttpPost]
        public JsonResult GenerateLink(string url, string referralId) {
            if (!url.StartsWith("http")) url = "http://" + url;

            var uri = new Uri(url);
            var root = uri.GetLeftPart(UriPartial.Authority);
            var admin = new Administration();
            var site = admin.GetSite(url);
            var retVal = "";

            if (site != null) {
                var path = uri.PathAndQuery;

                if (path.StartsWith("/")) path = path.Substring(1);
                var pattern = site.ReferralLinkPattern;
                var link = pattern
                                .Replace("{root}", root)
                                .Replace("{path}", path)
                                .Replace("{id}", referralId);

                retVal = link;
            }
            else {
                retVal = "Site is not registered on the application";
            }

            return Json(new { link = retVal });
        }

    }
}
